package io.renren.modules.erp.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.DateFormatUtil;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.erp.entity.BusinessBillEntity;
import io.renren.modules.erp.entity.BusinessBillVO;
import io.renren.modules.erp.service.BusinessBillInfoService;
import io.renren.modules.erp.service.BusinessBillService;



/**
 * 客户订单
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-07-21 14:03:08
 */
@RestController
@RequestMapping("erp/businessbill")
public class BusinessBillController {
    @Autowired
    private BusinessBillService businessBillService;
    
    @Autowired
    private BusinessBillInfoService businessBillInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("erp:businessbill:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = businessBillService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{businessBillId}")
    @RequiresPermissions("erp:businessbill:info")
    public R info(@PathVariable("businessBillId") Long businessBillId){
        BusinessBillEntity businessBill = businessBillService.selectById(businessBillId);

        return R.ok().put("businessBill", businessBill);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("erp:businessbill:save")
    public R save(@RequestBody BusinessBillVO businessBillVO){
    	String billDateStr=businessBillVO.getBillDateStr();
    	Date billDate=DateFormatUtil.parseStrToDate(billDateStr,DateFormatUtil.DATE_FORMAT_YYYY_MM_DD);
    	
    	BusinessBillEntity businessBill = new BusinessBillEntity();
        BeanUtils.copyProperties(businessBillVO,businessBill);
        businessBill.setBillDate(billDate);
        businessBillService.insert(businessBill);

        Long business_bill_info_id = businessBill.getBusinessBillInfoId();
        //更新总金额和总数量
        businessBillInfoService.updateTotalNumAndPrice(business_bill_info_id);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("erp:businessbill:update")
    public R update(@RequestBody BusinessBillVO businessBillVO){
    	String billDateStr=businessBillVO.getBillDateStr();
    	Date billDate=DateFormatUtil.parseStrToDate(billDateStr,DateFormatUtil.DATE_FORMAT_YYYY_MM_DD);
    	
    	BusinessBillEntity businessBill = new BusinessBillEntity();
        BeanUtils.copyProperties(businessBillVO,businessBill);
        businessBill.setBillDate(billDate);
        ValidatorUtils.validateEntity(businessBill);
        
        businessBillService.updateAllColumnById(businessBill);//全部更新
        Long business_bill_info_id = businessBill.getBusinessBillInfoId();
        //更新总金额和总数量
        businessBillInfoService.updateTotalNumAndPrice(business_bill_info_id);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("erp:businessbill:delete")
    public R delete(@RequestBody Long[] businessBillIds){
        businessBillService.deleteBatchIds(Arrays.asList(businessBillIds));

        return R.ok();
    }

    /**
     * 列表by订单
     */
    @RequestMapping("/listbybill")
    @RequiresPermissions("erp:businessbillinfo:list")
    public R listbybill(@RequestParam Map<String, Object> params){
        List<BusinessBillEntity> list = businessBillService.listbybill(params);

        return R.ok().put("list", list);
    }
    
}
